﻿/* 
 *	Copyright (C) 2010- ZWaveApi
 *	http://ZWaveApi.net
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU Lesser General Public License as published by
 *  the Free Software Foundation; either version 3, or (at your option)
 *  any later version.
 *   
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *   
 *  You should have received a copy of the GNU Lesser General Public License
 *  along with GNU Make; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. 
 *  http://www.gnu.org/copyleft/lesser.html
 *
 */

using System.Windows;
using System.Windows.Controls;
using ZWaveApi.ZWaveApiServiceReference;
using System.ComponentModel;
using ZWaveApi.Controls;

namespace ZWaveApi
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();

            ZWaveApiServerClient zWaveApiClient = new ZWaveApiServerClient();

            zWaveApiClient.IsOpenCompleted += delegate(object sender1, IsOpenCompletedEventArgs e1)
            {
                ZWaveApiStatus(e1.Result);
            };

            zWaveApiClient.IsOpenAsync();

            zWaveApiClient.NodeIsUpdateReceived += NodeIsUpdateReceived;
            zWaveApiClient.LogIsUpdateReceived += LogIsUpdateReceived;
            zWaveApiClient.RegisterForUpdatesAsync();
        }

        private void NodeIsUpdateReceived(object sender, NodeIsUpdateReceivedEventArgs e)
        {
            byte nodeID = e.nodeID;

            ctlGeneric zWaveControl = (ctlGeneric)this.FindName("ctl" + e.commandClass.ToString());

            if ((zWaveControl != null) && (zWaveControl.Visibility == System.Windows.Visibility.Visible))
            {
                zWaveControl.GetState(nodeId: e.nodeID, commandClass: e.commandClass);
            }
        }

        private void LogIsUpdateReceived(object sender, LogIsUpdateReceivedEventArgs e)
        {
            txtLog.Text = txtLog.Text + e.msg + '\n';
        }

        private void btnStartZWaveApiServer_Click(object sender, RoutedEventArgs e)
        {
            txtStatusZWaveApiServer.Text = "I am starting, plaese wait";

            ZWaveApiServerClient zWaveApiClient = new ZWaveApiServerClient();

            zWaveApiClient.OpenCompleted += delegate(object sender1, OpenCompletedEventArgs e1)
            {
                ZWaveApiStatus(e1.Result);
            };

            zWaveApiClient.OpenAsync();
        }

        private void btnGetAllNodes_Click(object sender, RoutedEventArgs e)
        {
            ZWaveApiServerClient zWaveApiClient = new ZWaveApiServerClient();

            zWaveApiClient.IsOpenCompleted += delegate(object sender1, IsOpenCompletedEventArgs e1)
            {
                ZWaveApiStatus(e1.Result);

                if (e1.Result)
                    showNodes();
            };

            zWaveApiClient.IsOpenAsync();
        }

        private void btnStopZWaveApiServer_Click(object sender, RoutedEventArgs e)
        {
            ZWaveApiServerClient zWaveApiClient = new ZWaveApiServerClient();

            zWaveApiClient.CloseCompleted += delegate(object sender1, AsyncCompletedEventArgs e1)
            {
                if (trvZWaveNodes.Items.Count > 0)
                    trvZWaveNodes.Items.Clear();

                ZWaveApiStatus(false);
            };

            zWaveApiClient.CloseAsync();
        }

        private void showNodes()
        {
            if (trvZWaveNodes.Items.Count > 0)
                trvZWaveNodes.Items.Clear();

            ZWaveApiServerClient zWaveApiClient = new ZWaveApiServerClient();

            zWaveApiClient.GetAllNodesCompleted += delegate(object sender1, GetAllNodesCompletedEventArgs e1)
            {
                if (e1.Result != null)
                {
                    foreach (ZWaveNode zWaveNode in e1.Result)
                    {
                        TreeViewItem tvZWaveNode = new TreeViewItem();

                        tvZWaveNode.HeaderTemplate = (DataTemplate)this.Resources["TreeViewItemZWaveNode"];
                        tvZWaveNode.Header = zWaveNode;

                        if (zWaveNode.ListOfCommandClasses.Count > 0)
                        {
                            foreach (GenericCommandClass commandClass in zWaveNode.ListOfCommandClasses)
                            {
                                TreeViewItem tvCommandClass = new TreeViewItem();

                                tvCommandClass.HeaderTemplate = (DataTemplate)this.Resources["TreeViewItemCommandClass"];
                                tvCommandClass.Header = commandClass;

                                tvZWaveNode.Items.Add(tvCommandClass);
                            }
                        }

                        trvZWaveNodes.Items.Add(tvZWaveNode);
                    }
                }
            };

            zWaveApiClient.GetAllNodesAsync();
        }

        private void ZWaveApiStatus(bool isOpen)
        {
            if (isOpen)
                txtStatusZWaveApiServer.Text = "Started";
            else
            {
                txtStatusZWaveApiServer.Text = "Stopped";
                CollapsetAllControls();
            }
        }

        private void trvZWaveNodes_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue != null)
            {
                object zWaveObject = ((TreeViewItem)e.NewValue).Header;

                string zWaveObjectName = zWaveObject.ToString();

                if (zWaveObjectName.LastIndexOf('.') != -1)
                {
                    CollapsetAllControls();

                    switch (zWaveObjectName.Substring(zWaveObjectName.LastIndexOf('.') + 1))
                    {
                        case "ZWaveNode":
                            break;
                        case "SwitchBinary":
                            CommandClassClient ccc = new CommandClassClient();

                            SwitchBinary sw = (SwitchBinary)zWaveObject;

                            ccc.RequestStateCompleted += delegate(object sender1, AsyncCompletedEventArgs e1)
                                {
                                    ctlSwitchBinary.DataContext = zWaveObject;
                           //       ctlSwitchBinary.GetState(sw.NodeId, sw.CommandClass);
                                    ctlSwitchBinary.Visibility = System.Windows.Visibility.Visible;
                                };

                            ccc.RequestStateAsync(sw.NodeId, sw);
                            break;
                        default:
                            break;
                    }
                }
            }
        }

        private void btnZWaveInfo_Click(object sender, RoutedEventArgs e)
        {
            ZWaveApiServerClient zWaveInfoClient = new ZWaveApiServerClient();

            CollapsetAllControls();

            zWaveInfoClient.InfoCompleted += delegate(object sender1, InfoCompletedEventArgs e1)
            {
                if (e1.Result != null)
                {
                    ctlZWaveInfo.DataContext = (ZWaveInfo)e1.Result;
                    ctlZWaveInfo.Visibility = System.Windows.Visibility.Visible;
                }
            };

            zWaveInfoClient.InfoAsync();
        }

        private void CollapsetAllControls()
        {
            ctlZWaveInfo.Visibility = System.Windows.Visibility.Collapsed;
            ctlSwitchBinary.Visibility = System.Windows.Visibility.Collapsed;
        }
    }
}
